<?php
    /* 
    *接口说明:  订餐统计信息上传
    *创建者: 	kai
    *时间:	  	20180319
    */
    //echo "<meta http-equiv='Content-Type'' content='text/html; charset=utf-8'>";
    // 设置时区
    date_default_timezone_set('Etc/GMT-8');
    require_once 'commom/dbfunction.php';
    require_once 'commom/config.php';
    $arrResult = null;
    $conn = null;
    do{
        // 参数检查
        $json_infoString = isset($_GET['info']) ? $_GET['info'] : '';
        if(empty($json_infoString)){
            $arrResult = array(
                        "errcode" => 1,
                        "info" => "get参数为空",
                        );
            break;
        }

        // 从传入参数中获取需要的信息
        $json_info = json_decode($json_infoString, true);
        // 获取信息
        $shareuniqueid = "";
        $publisheropenid = "";
        $orderopenid = "";
        $addtime = "";
        $name = "";
        $icon = "";
        $formid = "";
        $R1 = 0; 
        $R2 = 0;
        $R3 = 0;
        $R4 = "";
        $R5 = "";
		if(isset($json_info['shareuniqueid'])){
			$shareuniqueid = $json_info['shareuniqueid'];
        }
        if(isset($json_info['publisheropenid'])){
			$publisheropenid = $json_info['publisheropenid'];
        }
        if(isset($json_info['orderopenid'])){
			$orderopenid = $json_info['orderopenid'];
        }
        if(isset($json_info['addtime'])){
			$addtime = $json_info['addtime'];
        }
        if(isset($json_info['name'])){
			$name = $json_info['name'];
        }
        if(isset($json_info['icon'])){
			$icon = $json_info['icon'];
        }
        if(isset($json_info['formid'])){
            $formid = $json_info['formid'];
        }

        // 获取数据库链接
        // 创建连接
        $conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD);
        // 检测连接
        if (!$conn) {
            $arrResult = array(
                "errcode" => 2,
                "info" => "连接mysql数据库失败：". mysqli_connect_errno().":".mysqli_connect_error(),
            );
            break;
        }
        mysqli_set_charset($conn, "utf8");
        // 选择数据库
        if(!mysqli_select_db($conn, DB_DBNAME)){
            $arrResult = array(
                "errcode" => 3,
                "info" => "选择mysql数据库失败：". mysqli_connect_errno().":".mysqli_connect_error(),
            );
            break;
        }

        // 根据shareuniqueid和publisheropenid获取分享的记录
        $QueryOrderStatSql = "select * from orderstat where shareuniqueid='" . $shareuniqueid . "' and publisheropenid='" . $publisheropenid . "'";
        $result = mysqli_query($conn, $QueryOrderStatSql);
        if(!$result){
            $arrResult = array(
                "errcode" => 4,
                "info" => "查询orderstat数据库失败。shareuniqueid=". $shareuniqueid . "; publisheropenid=". $publisheropenid,
            );
            break;
        }

        $row = mysqli_fetch_assoc($result);
        if(!$row){
            $arrResult = array(
                "errcode" => 5,
                "info" => "未找到指定的订餐分享。shareuniqueid=". $shareuniqueid . "; publisheropenid=". $publisheropenid,
            );
            break;
        }
        // 先释放掉资源
        mysqli_free_result($result);

        // 判断是否合法
        $CurrentTime = date("y-m-d h:i:s");
        $EndTime = $row['enddate'] . ' '. $row['endtime'] . ':00';
        // 结束时间小于当前时间了，不合法
        if(strtotime($EndTime)<strtotime($CurrentTime)){
            $arrResult = array(
                "errcode" => 6,
                "info" => "当前时间超过了最后期限：EndTime=". $EndTime . "; CurrentTime=". $CurrentTime,
            );
            break;
        }

        // 查询orderstatshare表是否已经添加了
        $QueryIsExistInOrderStatSql = "select * from orderstatshare where orderstatno=".$row['no']." and wechatopenid='". $orderopenid ."'";
        $result1 = mysqli_query($conn, $QueryIsExistInOrderStatSql);
        if(!$result1){
            $arrResult = array(
                "errcode" => 4,
                "info" => "查询orderstatshare数据库失败。orderstatno=". $row['no'] . "; wechatopenid=". $orderopenid,
            );
            break;
        }
        // 判断是否已经有该记录了
        if (mysqli_num_rows($result1) > 0){
            $arrResult = array(
                "errcode" => 8,
                "info" => "该用户已经添加过了。sql=".$QueryIsExistInOrderStatSql,
            );
            break;
        }

        // 将数据插入到share表中
        $InsertOrderStatShareSql = "INSERT INTO orderstatshare (orderstatno, orderstatuniqueid, publisheropenid, wechatopenid, addtime, name, icon, formid, R1, R2, R3, R4, R5)
                        values(".$row['no'].",'".$shareuniqueid."','".$publisheropenid."','".$orderopenid."','".$addtime."','".$name."','".$icon."','".$formid."',".$R1.",".$R2.",".$R3.",'".$R4."','".$R5."')";
        
        if(!mysqli_query($conn, $InsertOrderStatShareSql)){
            $arrResult = array(
                "errcode" => 7,
                "info" => "插入到添加人表失败：sql=". $InsertOrderStatShareSql . "; ErrorInfo=". mysqli_error($conn),
            );
            break;
        }

        // 输出结果
		$arrResult = array(
					"errcode" => 0,
					"sql" => $InsertOrderStatShareSql,
                    );
                    
        // 通知用户
    }while(false);

    // 数据库链接清理
    if($conn){
        mysqli_close($conn);
    }

    // 输出结果
    echo json_encode($arrResult);
?>